package com.itita.ww2.utils;

import java.sql.Timestamp;
import java.util.Locale;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TimeUtil {
	public static final DateTimeFormatter redisTimestampFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取指定时区的指定时间点
	 */
	public static final DateTime getDateTimeWithZoneTimeOfDay(String zoneId, int hour, int minute, int second,
			int millis) {
		DateTime dt = DateTime.now(DateTimeZone.forTimeZone(TimeZone.getTimeZone(zoneId)));
		dt = dt.withHourOfDay(hour).withMinuteOfHour(minute).withSecondOfMinute(second).withMillisOfSecond(millis);
		return dt;
	}

	/**
	 * 现在是否在给定时段内
	 * 格式 09:00
	 * TimeZone.forID("GMT+8:00")
	 * ID : GMT+8:00
	 */
	public static final boolean timePeriod(String startTime, String endTime, String ID) {
		int startHours = Math.min(Integer.valueOf(startTime.split(":")[0]), 23);
		int startMinute = Math.min(Integer.valueOf(startTime.split(":")[1]), 59);
		DateTime start = getDateTimeWithZoneTimeOfDay(ID, startHours, startMinute, 0, 0);
		//
		int endHours = Math.min(Integer.valueOf(endTime.split(":")[0]), 23);
		int endMinute = Math.min(Integer.valueOf(endTime.split(":")[1]), 59);
		DateTime end = getDateTimeWithZoneTimeOfDay(ID, endHours, endMinute, 0, 0);

		DateTime now = DateTime.now(DateTimeZone.forTimeZone(TimeZone.getTimeZone(ID)));

		return now.isAfter(start) && now.isBefore(end) || (now.isEqual(start) || now.isEqual(end));
	}

	/**
	 * 疲劳购买次数是否能重置
	 */
	public static final boolean isFatigueReset(long lastTime, String zoneId, String resetTime) {
		int resetHours = Math.min(Integer.valueOf(resetTime.split(":")[0]), 23);
		int resetMinute = Math.min(Integer.valueOf(resetTime.split(":")[1]), 59);
		long reset = getDateTimeWithZoneTimeOfDay(zoneId, resetHours, resetMinute, 0, 0).getMillis();
		// 如果上次购买时间在重置时间点之前且本次时间在重置时间点之后(含),刷新次数
		return lastTime < reset && getUtcNowMillis() >= reset;
	}

	/**
	 * 是否还在cd中
	 * 特殊地: 若start为null,返回true
	 * (均用UTC时间)
	 */
	public static final boolean isCooldown(DateTime start, int duration) {
		if (start == null) {
			return false;
		}
		DateTime endTime = start.plusSeconds(duration);
		DateTime nowTime = DateTime.now(DateTimeZone.UTC);
		return nowTime.isBefore(endTime) || nowTime.isEqual(endTime);
	}

	/**
	 * 时间是否已到
	 */
	public static final boolean timesUp(long startTime, int duration) {
		return (startTime + duration) <= getUtcNowMillis();
	}

	public static final String getNowDayOfWeekShortText() {
		return getUtcNow().dayOfWeek().getAsShortText(Locale.ENGLISH);
	}

	/**
	 *  获取UTC时间
	 *  DateTime
	 */
	public static final DateTime getUtcNow() {
		return DateTime.now(DateTimeZone.UTC);// 获取UTC时间
	}

	public static final long getUtcNowMillis() {
		return getUtcNow().getMillis();
	}

	/**
	 * 获取UTC时间戳
	 */
	public static final Timestamp getTimestampUtcNow() {
		return new Timestamp(DateTime.now(DateTimeZone.UTC).getMillis());
	}
}
